package juliusapp.view.user;


import juliusapp.controller.UserController;
import juliusapp.model.UserVO;
import juliusapp.sys.main.R;
import juliusapp.util.dialog.OkDialog;
import juliusapp.util.validator.EmailValidator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ActivityUserRegister  extends Activity{
	
	private EditText edtName;
	private EditText edtEmail;
	private EditText edtPassword;
	private EditText edtPassword2;
	private Button btnRegister;
	
	private UserVO vo;
	private UserController userController;
	
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {	
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		edtName = (EditText) findViewById(R.id.reg_fullname);
		edtEmail = (EditText) findViewById(R.id.reg_email);
		edtPassword = (EditText) findViewById(R.id.reg_password);
		edtPassword2 = (EditText) findViewById(R.id.reg_password2);
		btnRegister = (Button) findViewById(R.id.btnRegister);
		btnRegister.requestFocus();
		
		userController = new UserController(getBaseContext());
		
		btnRegister.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				try {
					btnRegisterAction();
				} catch (Exception e) {
					goToDialogs(e.getMessage());
				}
			}
		});
	}
	
	
	private boolean canRegister() throws Exception{
		try {
			
			if(edtName.getText().toString().isEmpty()){				
				edtName.setError(getResources().getText(R.string.requiredName));
				edtName.requestFocus();
				return false;
			}
			else if(edtEmail.getText().toString().isEmpty()){
				edtEmail.setError(getResources().getText(R.string.requiredEmail));
				edtEmail.requestFocus();
				return false;
			}
			else if(EmailValidator.isInvalid(edtEmail.getText().toString())){
				edtEmail.setError(getResources().getText(R.string.invalidEmail));
				edtEmail.requestFocus();
				return false;
			}
			else if(edtPassword.getText().toString().isEmpty()){
				edtPassword.setError(getResources().getText(R.string.requiredPassword));
				edtPassword.requestFocus();
				return false;
			}
			else if(edtPassword2.getText().toString().isEmpty()){
				edtPassword2.setError(getResources().getText(R.string.requiredPassword));
				edtPassword2.requestFocus();
				return false;
			}
			else if(!edtPassword.getText().toString().equalsIgnoreCase(edtPassword2.getText().toString())){
				edtPassword2.setError(getResources().getText(R.string.notMatchPassword));
				edtPassword2.requestFocus();
				return false;
			}
			
			return true;
		} catch (Exception e) {
			throw new Exception("validLogin()|"+e.getMessage());
		}
		
	}
	
	
	/**
	 * Ação do botão cadastrar
	 * Verifica se existe um e-mail já cadastrado. caso não exista, insere o usuário e chama ação do botão voltar
	 * para que o login seja feito.
	 * @throws Exception
	 */
	protected void btnRegisterAction() throws Exception{
		try {
			if(canRegister()){
				Toast t;
				vo = new UserVO();
				vo.setName(edtName.getText().toString());
				vo.setEmail(edtEmail.getText().toString());
				vo.setPassword(edtPassword.getText().toString());
				if(userController.verifyEmail(vo)){					
					t = Toast.makeText(getBaseContext(), getResources().getString(R.string.insertSucess),Toast.LENGTH_SHORT);
					t.show();
					super.onBackPressed();
				}else
				{
					t = Toast.makeText(getBaseContext(), getResources().getString(R.string.existEmail),Toast.LENGTH_SHORT);
					t.show();
				}
	
			}
		} catch (Exception e) {
			throw new Exception("btnRegister|"+e.getMessage());
		}
		
		
	}

	private void goToDialogs(String msg) {
		OkDialog.show(msg, this);
	}

}
